home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Sound / AlgoMusic / Developer / Example2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-02  |  9.4 KB  |  383 lines

  1. /* 
  2.  
  3.    Example2.c   (C) 1997 by Thomas Schürger
  4.  
  5.    Last change: 30-Nov-97
  6.  
  7.    This is a small example how to use the AlgoMusicMsgPort structure
  8.    in order to obtain song information from AlgoMusic.
  9.  
  10.    Note that this thing was hacked up in a few minutes, it is rather
  11.    a bad example, but shows the way to access the data anyway.
  12.  
  13.    This example requires OS 3.0 or higher (V39), because of using
  14.    SetRGB32(). Don't blame me for that, blame yourself for having
  15.    an obsolete OS, if you don't have OS 3.0+.
  16.  
  17.    May be called with argument WINDOW to open a window instead of a
  18.    screen.
  19.  
  20.  
  21.    If you want a program that runs even though AlgoMusic is not, you
  22.    must wait for AlgoMusic's port to (re)appear after having got
  23.    a quit signal instead of exiting then.
  24.  
  25.    IF YOU'VE WRITTEN A BETTER EXAMPLE THAN THIS ONE, DON'T HESITATE
  26.    TO SEND ME THE SOURCECODE FOR INCLUSION IN THE NEXT RELEASE!
  27.  
  28.    Also read the developer section in the guide!
  29.  
  30. */
  31.  
  32.  
  33. #include <intuition/intuition.h>
  34.  
  35. #include <proto/exec.h>
  36. #include <proto/dos.h>
  37. #include <proto/intuition.h>
  38. #include <proto/graphics.h>
  39. #include <pragmas/exec_pragmas.h>
  40. #include <pragmas/dos_pragmas.h>
  41. #include <pragmas/intuition_pragmas.h>
  42. #include <pragmas/graphics_pragmas.h>
  43.  
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47.  
  48. #include "algomusic.h"
  49.  
  50. struct Window *win;
  51. struct Screen *screen;
  52. struct RastPort *rp;
  53. struct ViewPort *vp;
  54. struct MsgPort *mymsgport;
  55.  
  56. SHORT Last[SAMPLES],Current[SAMPLES];
  57.  
  58. BOOL algoquit;
  59.  
  60. ULONG width;
  61. ULONG height;
  62.  
  63. int r=0,g=0,b=0;                        /* Values for red, green, blue */
  64.  
  65. ULONG barheight;
  66. ULONG spaceheight;
  67. ULONG barspaceheight;
  68. BOOL window;
  69.  
  70. ULONG xoffset,yoffset;
  71.  
  72. USHORT sig[4];
  73. USHORT sigquit;
  74. ULONG  allsigs;
  75.  
  76. BOOL registered;
  77.  
  78. struct IntuiMessage *msg;
  79. struct AlgoMusicMsgPort *amport;
  80. struct AlgoMusicVoice *v;
  81.  
  82. struct IntuitionBase *IntuitionBase;
  83. struct GfxBase *GfxBase;
  84.  
  85. int  MyMin(int,int);
  86. void Add(int,int,int);
  87. void Init(void);
  88. void DeInit(void);
  89. void Communicate(struct MsgPort *port,struct MsgPort *myport,ULONG command,ULONG value1,ULONG value2,char *string1,char *string2,void *address);
  90.  
  91. int MyMin(int a1,int b1)
  92.   {
  93.   if(a1>b1) return(b1);
  94.   return(a1);
  95.   }
  96.  
  97. void Add(int a1,int b1,int c1)   /* Adds values to the background color */
  98.   {
  99.   int vol=v->Volume;
  100.  
  101.   r=MyMin(255,r+((vol*a1)>>6));
  102.   g=MyMin(255,g+((vol*b1)>>6));
  103.   b=MyMin(255,b+((vol*c1)>>6));
  104.   }
  105.  
  106. main(int argc,char **argv)
  107.   {
  108.   USHORT num;
  109.   int i; 
  110.   struct AlgoMusicMsgPort myport;
  111.   BYTE oldpri;
  112.   ULONG class;
  113.   ULONG sigs;
  114.  
  115.   if(argc==2 && argv[1][0]=='?')
  116.     {
  117.     printf("Usage: %s [WINDOW]\n",argv[0]);
  118.     exit(0);
  119.     }
  120.   else if(argc==2 && !stricmp(argv[1],"WINDOW"))
  121.     window=TRUE;
  122.  
  123.   Init();
  124.  
  125.   oldpri=SetTaskPri(FindTask(NULL),10);
  126.  
  127.   SetSignal(0,(1<<sig[0])|(1<<sig[1])|(1<<sig[2])|(1<<sig[3]));
  128.        /* Clear the voice signals that might have arrived already */
  129.  
  130.   while(!CheckSignal(SIGBREAKF_CTRL_C))
  131.     {
  132.     WaitTOF();        
  133.  
  134.     Forbid();
  135.     myport=*amport;   /* Make a copy of the AlgoMusicMsgPort */
  136.     Permit();         /* And ONLY use the copy */
  137.  
  138.     sigs=SetSignal(0,allsigs);  /* Get (all) signals, clear our 5 ones */
  139.  
  140.     if(sigs&(1<<sigquit))   /* AlgoMusic sent us a QUIT? */
  141.       {
  142.       algoquit=TRUE;
  143.       SetTaskPri(FindTask(NULL),oldpri);
  144.       DeInit();
  145.       }
  146.       
  147.     for(i=0;i<4;i++)
  148.       {
  149.       if(sigs&(1<<sig[i]))   /* Has the specific signal been sent? */
  150.         {
  151.         v=&myport.Voice[i];  /* Get AlgoMusicVoice structure */
  152.         num=v->SampleNumber; /* Get sample number */
  153.  
  154.         if(num==BASE || num==BASECLAP)
  155.           Add(255,0,0);   
  156.  
  157.         if(num==CLAP || num==BASECLAP || num==CLAPHIHAT)
  158.           Add(0,255,0);
  159.  
  160.         if(num<=5)          /* That's all kinds of chords */
  161.           Add(0,0,255);
  162.         else if(num==BASS)
  163.           Add(200,0,230);            
  164.         else if(num==ACID)
  165.           Add(40,100,40);            
  166.         else if(num==MELODY)
  167.           Add(100,0,50);            
  168.         else if(num==SNARE)
  169.           Add(120,120,120);            
  170.  
  171.         Current[num]=(v->Volume*4*width)/256-1;
  172.         if(Current[num]<0) Current[num]=0;
  173.         }
  174.       }
  175.  
  176.     if(!window) SetRGB32(vp,0,r<<24,g<<24,b<<24);
  177.  
  178.     r-=20;if(r<0) r=0;
  179.     g-=20;if(g<0) g=0;    /* Decrease RGB values */
  180.     b-=20;if(b<0) b=0; 
  181.  
  182.     for(i=0;i<SAMPLES;i++)        /* Loop for updating the bars */
  183.       {
  184.       if(Last[i]>Current[i])      /* Must we shorten the bar? */
  185.         {
  186.         SetAPen(rp,0);
  187.         RectFill(rp,xoffset+Current[i]+1,yoffset+i*barspaceheight,xoffset+Last[i],yoffset+i*barspaceheight+barheight);
  188.         }
  189.       else if(Last[i]<Current[i]) /* Must we append to the bar? */
  190.         {
  191.         SetAPen(rp,1);
  192.         RectFill(rp,xoffset+Last[i]+1,yoffset+i*barspaceheight,xoffset+Current[i],yoffset+i*barspaceheight+barheight);
  193.         }
  194.       Last[i]=Current[i];
  195.  
  196.       Current[i]-=3;               /* Decrease bar amplitude */
  197.       if(Current[i]<0) Current[i]=0;
  198.       }
  199.  
  200.     if(window)                /* Window message handling */
  201.       {
  202.       while(msg=(struct IntuiMessage *)GetMsg(win->UserPort))
  203.         {
  204.         class=msg->Class;
  205.         ReplyMsg(msg);
  206.  
  207.         if(class==IDCMP_CLOSEWINDOW)
  208.           {
  209.           SetTaskPri(FindTask(NULL),oldpri);
  210.           DeInit();
  211.           }
  212.         else if(class=IDCMP_NEWSIZE)
  213.           {
  214.           width=win->Width-win->BorderLeft-win->BorderRight;
  215.           height=win->Height-win->BorderTop-win->BorderBottom-1;
  216.           barspaceheight=height/SAMPLES;
  217.           barheight=(barspaceheight*2)/3;
  218.           spaceheight=barspaceheight-barheight;
  219.  
  220.           SetAPen(rp,0);
  221.           RectFill(rp,xoffset,yoffset,xoffset+width-1,yoffset+height-1);
  222.           SetAPen(rp,1);
  223.           for(i=0;i<SAMPLES;i++)
  224.             {
  225.             Last[i]=0;
  226.             Current[i]=0;
  227.             RectFill(rp,xoffset+0,yoffset+i*barspaceheight,xoffset+0,yoffset+i*barspaceheight+barheight);
  228.             }
  229.           }
  230.         }
  231.       }
  232.     }
  233.  
  234.   SetTaskPri(FindTask(NULL),oldpri);
  235.  
  236.   DeInit();
  237.   }
  238.  
  239. void Init()
  240.   {
  241.   int i;
  242.  
  243.   if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",39)))
  244.     {
  245.     printf("Couldn't open intuition.library V39!\n");
  246.     DeInit();
  247.     }
  248.  
  249.   if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",39)))
  250.     {
  251.     printf("Couldn't open intuition.library V39!\n");
  252.     DeInit();
  253.     }
  254.  
  255.   if(!(amport=(struct AlgoMusicMsgPort *)FindPort(ALGOMUSICPORTNAME)))
  256.     {
  257.     printf("AlgoMusic is not running!\n");
  258.     DeInit();
  259.     }
  260.  
  261.   if(!(mymsgport=CreatePort(0,0)))
  262.     {
  263.     printf("Couldn't allocate message port!\n");
  264.     DeInit();
  265.     }
  266.  
  267.   sig[0] =AllocSignal(-1);
  268.   sig[1] =AllocSignal(-1);
  269.   sig[2] =AllocSignal(-1);
  270.   sig[3] =AllocSignal(-1);
  271.   sigquit=AllocSignal(-1);
  272.  
  273.   if(!sig[0] || !sig[1] || !sig[2] || !sig[3] || !sigquit)
  274.     {
  275.     printf("Couldn't allocate signals!\n");
  276.     DeInit();
  277.     }
  278.  
  279.   Communicate(amport,mymsgport,COMMAND_REGISTER,(sig[0]<<24)|(sig[1]<<16)|(sig[2]<<8)|sig[3],sigquit,NULL,NULL,NULL);
  280.   registered=TRUE;
  281.  
  282.   allsigs=(1<<sig[0])|(1<<sig[1])|(1<<sig[2])|(1<<sig[3])|(1<<sigquit);
  283.  
  284.   if(!window && (screen=OpenScreenTags(NULL,SA_AutoScroll,TRUE,SA_Quiet,TRUE,SA_Overscan,OSCAN_STANDARD,TAG_DONE)))
  285.     {
  286.     rp=&screen->RastPort;
  287.     vp=&screen->ViewPort;
  288.  
  289.     SetRGB32(vp,0,0,0,0);
  290.     SetRGB32(vp,1,255<<24,255<<24,255<<24);
  291.  
  292.     width=screen->Width;
  293.     height=screen->Height;
  294.     }  
  295.   else if(win=OpenWindowTags(NULL,WA_Title,"Example 2",WA_ScreenTitle,"Example 2  (C) 1997 by Thomas Schürger (schuerge@studcs.uni-sb.de)",WA_MinHeight,60,WA_MinWidth,60,WA_SizeGadget,TRUE,WA_DragBar,TRUE,WA_DepthGadget,TRUE,WA_CloseGadget,TRUE,WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_NEWSIZE,TAG_DONE))
  296.     {
  297.     rp=win->RPort;
  298.     
  299.     width=win->Width-win->BorderLeft-win->BorderRight;
  300.     height=win->Height-win->BorderTop-win->BorderBottom-1;
  301.     }
  302.   else
  303.     {
  304.     printf("Could not open screen!\n");
  305.     DeInit();
  306.     }
  307.  
  308.   barspaceheight=height/SAMPLES;
  309.   barheight=(barspaceheight*2)/3;
  310.   spaceheight=barspaceheight-barheight;
  311.  
  312.   if(win)
  313.     {
  314.     xoffset=win->BorderLeft;
  315.     yoffset=win->BorderTop+1;
  316.     }
  317.  
  318.   for(i=0;i<SAMPLES;i++)
  319.     {
  320.     Last[i]=0;
  321.     Current[i]=0;
  322.  
  323.     SetAPen(rp,1);
  324.     RectFill(rp,xoffset+0,yoffset+i*barspaceheight,xoffset+0,yoffset+i*barspaceheight+barheight);
  325.     }
  326.   }
  327.  
  328. void DeInit()
  329.   {
  330.   if(registered && !algoquit)
  331.     Communicate(amport,mymsgport,COMMAND_UNREGISTER,0,0,NULL,NULL,NULL);
  332.  
  333.   if(screen)
  334.     CloseScreen(screen);
  335.  
  336.   if(win)
  337.     CloseWindow(win);
  338.  
  339.   if(mymsgport)
  340.     DeletePort(mymsgport);
  341.  
  342.   if(sig[0])  FreeSignal(sig[0]);
  343.   if(sig[1])  FreeSignal(sig[1]);
  344.   if(sig[2])  FreeSignal(sig[2]);
  345.   if(sig[3])  FreeSignal(sig[3]);
  346.   if(sigquit) FreeSignal(sigquit);
  347.  
  348.   if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  349.   if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  350.  
  351.   exit(0);
  352.   }
  353.  
  354. void Communicate(struct MsgPort *port,struct MsgPort *myport,ULONG command,ULONG value1,ULONG value2,char *string1,char *string2,void *address)
  355.   {
  356.   static struct AlgoMessage msg;
  357.  
  358.   msg.Message.mn_Node.ln_Succ=NULL;
  359.   msg.Message.mn_Node.ln_Pred=NULL;
  360.   msg.Message.mn_Node.ln_Pri=0;
  361.   msg.Message.mn_Node.ln_Name=NULL;
  362.   msg.Message.mn_ReplyPort=myport;
  363.   msg.Message.mn_Length=sizeof(struct AlgoMessage);
  364.  
  365.   msg.Command=command;
  366.   msg.Value1=value1;
  367.   msg.Value2=value2;
  368.  
  369.   if(string1)
  370.     strcpy((char *)&msg.String1[0],string1);
  371.   else
  372.     msg.String1[0]=0;
  373.  
  374.   if(string2)
  375.     strcpy((char *)&msg.String2[0],string2);
  376.   else
  377.     msg.String2[0]=0;
  378.  
  379.   msg.Address=address;
  380.  
  381.   PutMsg(port,(struct Message *)&msg);
  382.   }
  383.